package com.asialjim.wechat.core.api;

import com.asialjim.wechat.apiservice.enumeration.WeChatApiReturnEnumeration;
import com.asialjim.wechat.apiservice.exception.WeChatApiException;
import lombok.Data;

import java.util.Objects;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: BaseWeChatApiRes ]</i></h2><strong>
 * <p> base we chat api response
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/2/27   &nbsp;&nbsp; JDK 8
 */
@Data
public abstract class BaseWeChatApiRes<T> implements WeChatApiRes<T> {

    /**
     * <h3>FIELD DESCRIPTION</h3>
     * we chat api execute response error code
     */
    protected Integer errcode;

    /**
     * <h3>FIELD DESCRIPTION</h3>
     * we chat api execute response error message
     */
    protected String errmsg;

    @Override
    public boolean isSuccess() {
        return Objects.isNull(this.errcode) || WeChatApiReturnEnumeration.CODE_0.getCode() == this.errcode;
    }

    protected void validate(){
        if (isNotSuccess()){
            WeChatApiException.cast(String.valueOf(errcode),WeChatApiReturnEnumeration.codeOf(errcode).getMessage(),errmsg);
        }
    }

    @Override
    public T parse() {
        validate();
        return (T) this;
    }
}
